#!/usr/bin/env node
// @ts-nocheck

// 始终重新生成 expose
process.env.REEXPOSE = 'true';

process.on('unhandledRejection', (err) => {
  if (err.stderr) {
    return console.log(err.stderr);
  }
  throw err;
});


require('dotenv').config();


const { _: args, ...opt } = require('minimist')(process.argv.slice(2));

const { workerRun } = require('./lib/workerRun');
const { isCCWEBSplitProject, isCCWEB } = require('./lib/envUtil');

const { getDomainExposesCache, saveDomainExposes, saveRemoteExposes } = require('./lib/domain.expose.js');

// 域expose导出缓存
const DomainExposesCache = getDomainExposesCache();

// 执行命令
workerRun(require.resolve('./mod/expose.js'), {
  globPattern: 'src/**/*.{ts,tsx}',
  onResult: async (res) => {
    res.forEach((it) => {
      Object.keys(it).forEach((domain) => {
        it[domain].forEach((m) => {
          if(domain === 'thirdlib' && /^src\//.test(m)) {
            
          } else {
            DomainExposesCache[domain].exposes.add(m)
          }
        });
      });
    });
    // saas-cc-web拆分工程
    if (isCCWEBSplitProject()) {
      console.log('拆分工程,不生成expose');
      await saveRemoteExposes()
      return 'ok';
    }
    if(isCCWEB()) {
      await saveDomainExposes();
      return 'ok';
    }
  },
});
